﻿using SchoolCommunicationSystem.Models;
using SCS.Business;
using SCS.Common.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SchoolCommunicationSystem.Controllers
{
    public class TermController : Controller
    {
        private TermServices _termServices = new TermServices();

        /// <summary>
        /// Pre add a new term
        /// </summary>
        /// <returns>View</returns>
        public ActionResult AddTerm()
        {
            var term = new List<SelectListItem>();
            term.Add(new SelectListItem() { Text = "1", Value = "1" });
            term.Add(new SelectListItem() { Text = "2", Value = "2" });
            ViewBag._Term = term;

            IList<Term> terms = null;
            terms = _termServices.GetAll();

            var schoolYearName = _termServices.GetAllSchoolYear().Select(t => new SelectListItem()
            {
                Text = t.schoolYearName,
                Value = t.schoolYearID.ToString(),
            }).ToList();

            var data = new TermModel()
            {
                TermSelectList = ViewBag._Term,
                SchoolYearSelectList = schoolYearName,
                terms = terms
            };

            return View(data);
        }

        /// <summary>
        /// add a new term
        /// </summary>
        /// <param name="schooYearModel">TermModel</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddTerm(TermModel termModel)
        {
            if (ModelState.IsValid)
            {
                Term term = new Term();
                term.termName = termModel.termName;
                term.schoolYearID = termModel.SchoolYearId;
                if (_termServices.CheckTermName(term))
                {
                    _termServices.AddTerm(term);
                    return RedirectToAction("AddTerm");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Dữ liệu nhập đã tồn tại");

                    var termList = new List<SelectListItem>();
                    termList.Add(new SelectListItem() { Text = "1", Value = "1" });
                    termList.Add(new SelectListItem() { Text = "2", Value = "2" });
                    ViewBag._Term = termList;

                    IList<Term> termsList = null;
                    termsList = _termServices.GetAll();

                    var schoolYearNameList = _termServices.GetAllSchoolYear().Select(t => new SelectListItem()
                    {
                        Text = t.schoolYearName,
                        Value = t.schoolYearID.ToString(),
                    }).ToList();

                    var data = new TermModel()
                    {
                        TermSelectList = ViewBag._Term,
                        SchoolYearSelectList = schoolYearNameList,
                        terms = termsList
                    };

                    return View(data);
                }
            }
            var termss = new List<SelectListItem>();
            termss.Add(new SelectListItem() { Text = "1", Value = "1" });
            termss.Add(new SelectListItem() { Text = "2", Value = "2" });
            ViewBag._Term = termss;

            IList<Term> terms = null;
            terms = _termServices.GetAll();

            var schoolYearName = _termServices.GetAllSchoolYear().Select(t => new SelectListItem()
            {
                Text = t.schoolYearName,
                Value = t.schoolYearID.ToString(),
            }).ToList();
            termModel.SchoolYearSelectList = schoolYearName;

            termModel.TermSelectList = ViewBag._Term;
            termModel.SchoolYearSelectList = schoolYearName;
            termModel.terms = terms;
            return View(termModel);
        }
    }
}
